/**
 * request 封装网络请求 
 * 返回值 promise
 * 参数：object
 * url：请求地址
 * method: 请求方法 默认get
 * data: 参数
 */
import {request} from "../utils/util.js";

//获取轮播接口
export function getLunbos(){
  return request({
    url:"lunbo"
  })
}

//获取专业接口
export function getMajor(){
  return request({
    url:"major"
  })
}

//获取课程接口
export function getCourse(){
  return request({
    url:"course?limit=1000"
  })
}

//获取推荐课程接口
export function getTuijian(){
  return request({
    url:`course?sort={"_createTime":-1}&limit=1`,
  })
}

//点击课程修改访问量接口
export function clickCourse(id,count){
  return request({
    url:`course/${id}`,
    method:"PATCH",
    data:{
      "data": {
        "visit_count":count
      }
      
    }
  })
}

//获取所有视频接口
export function getAllVideos(){
  return request({
    url:'video?limit=10000'
  })
}

//获取关于我们接口
export function getGywm(){
  return request({
    url:'about'
  })
}

// 添加观看历史接口
export function addHistory(openid,course,video){
  return request({
    url:'visit_history',
    method:'post',
    data:{
      "data": [
        {
          "openid":openid,
          "course_id": course,
          "video_id":video,
          "createTime": Date.now(),
          "modifyTime": Date.now()
        }
      ]
    }
  })
}

// 修改某一个观看历史接口
export function updVideoHistory(id,course,video){
  return request({
    url:`visit_history/${id}`,
    method:"PATCH",
    data:{
      "data": {
          "course_id": course,
          "video_id":video,
          "modifyTime": Date.now()
      }
    }
  })
}

// 获取某一个视频的观看历史接口
export function getVideoHistory(openId,vid){
  return request({
    url:'visit_history/find',
    method:'post',
    data:{
      "query": {
        "openid":openId,
        "video_id":vid
      }
    }
  })
}

//获取所有观看历史接口
export function getHistory(openid,skip){
  return request({
    url:`visit_history/find?sort={"modifyTime":-1}&skip=${skip}&limit=10`,
    method:'post',
    data:{
      "query": {
        "openid":openid
        
      }
    }
  })
}

//删除观看历史接口
export function delHistory(id){
  return request({
    url:`visit_history/${id}`,
    method:'DELETE'
  })
}

//添加评价接口
export function addEvaluate(openid,course,content,count,name,img){
  return request({
    url:'evaluate',
    method:'post',
    data:{
      "data": [
        {
          "openid":openid,
          "course_id": course,
          "content":content,
          "star_count":count,
          "nickname":name,
          "pic_name":img,
          "createTime": Date.now(),
          "modifyTime": Date.now()
        }
      ]
    }
  })
}

//获取评价接口
export function getEvaluate(kcid,skip){
  return request({
    url: `evaluate/find?sort={"createTime":-1}&skip=${skip}&limit=10`,
    method:'post',
    data:{
      "query": {
        "course_id":kcid
      }
    }
   
  })
}

//获取资源接口
export function getResource(skip){
  return request({
    url:`resource?skip=${skip}&limit=10`,
  })
}

//添加收藏接口
export function addCollection(openId,course){
  return request({
    url:'collection',
    method:'post',
    data:{
      "data": [
        {
          "openid": openId,
          "course_id": course,
          "createTime": Date.now(),
          "modifyTime": Date.now()
        }
      ]
    }
  })
}

// 获取某一个收藏接口
export function getOneCollection(kcid,openid){
  return request({
    url:'collection/find',
    method:'post',
    data:{
      "query": {
        "course_id":kcid,
        "openid":openid
      }
    }
  })
}

// 删除收藏接口
export function delCollection(id){
  return request({
    url:`collection/${id}`,
    method:'DELETE'
  })
}

//获取我的收藏接口
export function getCollection(openid,skip){
  return request({
    url:`collection/find?sort={"createTime":-1}&skip=${skip}&limit=10`,
    method:'post',
    data:{
      "query": {
        "openid":openid
      }
    }
  })
}

//获取openid接口
export function getOpenid(code){
  return request({
    url:`wechat/getopenid.php?code=${code}`
  })
}

//获取用户接口
export function getUser(id){
  return request({
    url:`wechat_userr/find?limit=20`,
    method:'post',
    data:{
      "query": {
        "openid":id
      }
    }
  })
}

//添加用户接口
export function addUser(id){
  return request({
    url:`wechat_userr`,
    method:'post',
    data:{
      "data": [
        {
          "openid": id,
          "visit_count":0,
          "telephone":'',
          "createTime":Date.parse(new Date()),
          "modifyTime":Date.parse(new Date())
        }
      ]
    }
  })
}

//修改用户接口访问量
export function UdpUser(id,count){
  return request({
    url:`wechat_userr/${id}`,
    method:'PATCH',
    data:{
      "data": {
        "visit_count":count+1
      }
    }
  })
}

//修改用户接口手机号
export function UdpUserPhone(id,phone){
  return request({
    url:`wechat_userr/${id}`,
    method:'PATCH',
    data:{
      "data": {
        "telephone":phone
      }
    }
  })
}

//获取手机号接口
export function getPhoen(key,en,iv){
  return request({
    url:'wechat/getphone.php',
    method:'POST',
    data:{
      session_key: key,
      encryptedData: en,
      iv: iv
    },
    header: {
      'content-type':'application/x-www-form-urlencoded'
    }
  })
}

//获取我的收藏接口不限条数
export function getAllCollection(openid){
  return request({
    url:`collection/find?limit=1000`,
    method:'post',
    data:{
      "query": {
        "openid":openid
      }
    }
  })
}